//搜索框

new Vue({
    el: ".search-input",
    data() {
        return {
            show: true,
            keyword: '',
            placeholder: "搜索点什么吧",
            list: [],
            fade: false
        }
    },

    watch: {
        keyword() {
            this.show = !(this.keyword.length > 0 || this.keyword !== '');
            return this.keyword.replace(/\s*/g, "")
        },
        list() {
            this.list.length !== 0 ? this.fade = !0 : this.fade = !1
        }
    },
    mounted() {
        document.documentElement.onclick = (e) => {
            e.stopPropagation()
            this.close()
        }
        document.getElementById("keyword").onclick = e => {
            e.stopPropagation()
            this.list.length !== 0 ? this.fade = !0 : this.fade = !1
        }
    },
    methods: {
        close() {
            this.fade = !1
        },
        search(val) {
            window.open(`/search?word=${typeof (val) === 'string' ? val : this.keyword}`)
        },
        prev() {
            const id = null;
            clearTimeout(id)
            setTimeout(() => {
                const res = this.req();
                res.then(re => {
                    this.list = re.data
                })
            }, 210)
        },
        req() {
            return axios({
                url: "/search/prev",
                method: "POST",
                params: {
                    keyword: this.keyword
                }
            })
        }
    }
})


